package com.staples.tc.queueController;

import static com.staples.tc.util.TCIConstants.KEY_REVRECQ_CHANNEL;
import static com.staples.tc.util.TCIConstants.KEY_REVRECQ_HOST;
import static com.staples.tc.util.TCIConstants.KEY_REVRECQ_PORT;
import static com.staples.tc.util.TCIConstants.KEY_REVRECQ_QMGR;
import static com.staples.tc.util.TCIConstants.KEY_REVRECQ_QUEUE;

import org.apache.log4j.Logger;

import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import com.staples.tc.exception.CCardJobException;
import com.staples.tc.util.CCardJobUtil;
import com.staples.tc.util.TCIResourceBundle;

public class RevRecQConnManager {
	private static final Logger logger = Logger.getLogger(RevRecQConnManager.class);

	private static MQQueue revrecQ;
	private static MQQueueManager revrecQMgr;
	
	private void createRevRecQConnection() throws CCardJobException {
		logger.debug("Entering createRevRecQConnection() ....");
		if (revrecQMgr == null || revrecQ == null) {
			try {
				// RevRec Queue Configuration
				String host = TCIResourceBundle.getProperty(KEY_REVRECQ_HOST);
				String manager = TCIResourceBundle.getProperty(KEY_REVRECQ_QMGR);
				int port = Integer.parseInt(TCIResourceBundle.getProperty(KEY_REVRECQ_PORT));
				String channel = TCIResourceBundle.getProperty(KEY_REVRECQ_CHANNEL);
				String queue = TCIResourceBundle.getProperty(KEY_REVRECQ_QUEUE);

				MQEnvironment.hostname = host; // host to connect to
				MQEnvironment.channel = channel; // the CASE-SENSITIVE name of the SVRCONN channel
				MQEnvironment.port=port;
													
				revrecQMgr = new MQQueueManager(manager);
				int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT
						| MQC.MQOO_INQUIRE;
				revrecQ = revrecQMgr.accessQueue(queue, openOptions);
				logger.info("Queue connection success : " + host);
			} catch (MQException mqExp) {
				logger.error("MQException occured while getting Queue connection ..." + mqExp.getMessage());
				throw new CCardJobException(mqExp.getMessage(), mqExp);
			} catch (Exception exp) {
				logger.error("Exception occured while getting Queue connection ..." + exp.getMessage());
				throw new CCardJobException(exp.getMessage(), exp);
			}
		}
		logger.debug("Exiting createRevRecQConnection() ....");
	}
	
	public void writeMessage(String updatedMessage)throws CCardJobException {
		logger.debug("Entering REVREC writeMessage() ....");

		try {
			//Initialize Queue Connection
			this.createRevRecQConnection();

			logger.info("Current REVREC MQ Depth :" + revrecQ.getCurrentDepth());
			
			MQMessage qMsg = new MQMessage();
			qMsg.format = MQC.MQFMT_STRING; //Write as JMSTextMessage
			qMsg.writeString(updatedMessage.toString());

			// specify the message options...
//			logger.debug("Spcifying message options >>>");
			MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the
																	// //
																	// defaults,
																	// same as
																	// MQPMO_DEFAULT

			// put the message on the queue
			logger.debug("Sending message to the REVREC queue :" + CCardJobUtil.stripWhiteSpaces(updatedMessage));
			revrecQ.put(qMsg, pmo);
			logger.info("MESSAGE SENT SUCCESSFULLY >>>>>>>>>");
			logger.info("Current REVREC Queue depth After Writing Message :" + revrecQ.getCurrentDepth());
		} catch (MQException mqExp) {
			logger.error("MQException occured while writing into Queue ..." + mqExp.getMessage());
			throw new CCardJobException(mqExp.getMessage(), mqExp);
		} catch (Exception exp) {
			logger.error("Exception occured while writing into Queue ..." + exp.getMessage());
			throw new CCardJobException(exp.getMessage(), exp);
		} finally {
			this.closeAllQResources();
		}
	
		logger.debug("Exiting REVREC writeMessage() ....");
	}

	public void closeAllQResources() {
		try {
			if (revrecQ != null && revrecQ.isOpen()) {
				revrecQ.close();
				revrecQ = null;
			}
			if (revrecQMgr != null && revrecQMgr.isOpen()) {
				revrecQMgr.close();
				revrecQMgr = null;
			}
		} catch (Exception exp) {
			logger.error("Exception while closing MQ Resources ...", exp);
		}
	}
}
